package tmi.li.txwidgets.txwthr;


import tmi.li.txwidgets.TxWidgetsFinals;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import android.util.Log;

public class TxWthrReceiver extends BroadcastReceiver{
	static String TAG = "tmi.li.txwidgets.txwthr.TxWthrReceiver";
	
	@Override
    public void onReceive(Context context, Intent intent) {
		
		
		//getting the Broadcast Action to verify the intent Actions
        String action = intent.getAction();
        
        Log.d(TAG, "onReceive() intent => "+action);
        
        if (action.equals(Intent.ACTION_BOOT_COMPLETED) 
        	|| action.equals(Intent.ACTION_SCREEN_ON) 
        	|| action.equals(TxWidgetsFinals.TXWTHR_UPDATE)    ) {
        	Log.d(TAG, "onReceive() updating view");
        	
        	//get the app widgetmanager instance
            AppWidgetManager gm = AppWidgetManager.getInstance(context);
            
            int[] appWidgetIds = gm.getAppWidgetIds(new ComponentName(context.getApplicationInfo().packageName, TxWidgetsFinals.TXWTHR_WIDGET_CLASS_PATH));
            
            for(int i = 0; i < appWidgetIds.length; i++) {
            	TxWthrProvider.updateWidget(context, gm,  appWidgetIds[i]);
            }
        }
        if(action.equals(TxWidgetsFinals.TXWTHR_BUILD_UPDATE)) {
        	Log.d(TAG, "onReceive() updating data");
        	AppWidgetManager gm = AppWidgetManager.getInstance(context);
            int[] appWidgetIds = gm.getAppWidgetIds(new ComponentName(context.getApplicationInfo().packageName, TxWidgetsFinals.TXWTHR_WIDGET_CLASS_PATH));
            
            for(int i = 0; i < appWidgetIds.length; i++)  {
            	Log.d(TAG, "onReceive() updating data for id => "+appWidgetIds[i]);
            	Intent intents = new Intent(context, BuildService.class);
            	intents.putExtra("Id", appWidgetIds[i]);
            	context.startService(intents);
            }
        }
        
    }
	
	
}

